package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sravanns on 12/06/14.
 */
/* Job: represents book status(can be checked or removed or available)*/
public class Library {
    private  List<Book> checked_out_books = new ArrayList<Book>();
    private final List<Book> catalogue = new ArrayList<Book>() {{
        add(new Book("java", "james", 1880));
        add(new Book("c", "dennis", 1870));
        add(new Book("c++", "bala", 1890));
    }};
    private final List<Movie> movies = new ArrayList<Movie>() {{
        add(new Movie("m1", 2000,"d1", 0));
        add(new Movie("m2", 2001,"d2", 10));
        add(new Movie("m3",2002, "d3", 1));
    }};
    private  List<Movie> checked_out_movies = new ArrayList<Movie>();

    //private User logedInUser;

    private final Map<String,User> registeredMembers=new HashMap<String,User>(){{put("111-1111",new User("a","a@gmail.com","9999999999","a"));put("000-1111",new User("b","b@gmail.com","0000000000","b"));}};
    private Map<String,List<Book>> UserCheckedBooks = new HashMap<String, List<Book>>();
    private Map<String,List<Movie>> UserCheckedMovies = new HashMap<String, List<Movie>>();

    public List<Book> getChecked_out_books() {
        return checked_out_books;
    }

    public List<Book> getCatalogue() {
        return catalogue;
    }

    public boolean checkOutBook(final Book book, String user) {
        for (Book checkedBook:checked_out_books) {
            if(checkedBook.equals(book))
            {
                return false;
            }
        }
        checked_out_books.add(book);

            if((UserCheckedBooks.keySet()).contains(user))
            {
                UserCheckedBooks.get(user).add(book);
            }
           else
            {
                UserCheckedBooks.put(user,new ArrayList<Book>(){{add(book);}});


            }


        return true;


    }

    public boolean returnBook(Book book,String user) {
        try {
            if (UserCheckedBooks.keySet().contains(user)) {

                List<Book> checkedList = UserCheckedBooks.get(user);


                for (Book checkedbook : checkedList) {
                    if (checkedbook.equals(book)) {

                        UserCheckedBooks.get(user).remove(book);
                        checked_out_books.remove(book);
                        if (UserCheckedBooks.get(user).size() == 0) {
                            UserCheckedBooks.remove(user);
                        }
                        return true;

                    }
                }
            }



        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;

    }

    public Book getBookFromCatalogue(String name) {
        int pointer = 0;
        while (pointer < catalogue.size()) {
            if (name.equalsIgnoreCase(catalogue.get(pointer).getBook_name())) {
                return catalogue.get(pointer);
            }


            pointer++;
        }
        return null;
    }

    public List<Book> getAvailableBooks()
    {
        ArrayList<Book> availableBooks=new ArrayList<Book>();
        for (Book book :catalogue) {
            if(checked_out_books.contains(book))
            {
                continue;
            }
            availableBooks.add(book);

        }

        return availableBooks;
    }



    //movies
    public List<Movie> getChecked_out_Movies() {
        return checked_out_movies;
    }

    public List<Movie> getAllMovies() {
        return movies;
    }

    public boolean checkOutMovie(final Movie movie,String user) {
        for (Movie checkedMovie:checked_out_movies) {
            if(checkedMovie.equals(movie))
            {
               // checked_out_movies.remove(movie);
                return false;
            }
        }
        checked_out_movies.add(movie);
        if((UserCheckedMovies.keySet()).contains(user))
        {
            UserCheckedMovies.get(user).add(movie);
        }
        else
        {
            UserCheckedMovies.put(user,new ArrayList<Movie>(){{add(movie);}});


        }


        return true;

    }

    public boolean returnMovie(Movie movie,String user ) {
        if (UserCheckedMovies.keySet().contains(user)) {


            List<Movie> checkedList = UserCheckedMovies.get(user);
            for (Movie checkedmovie : checkedList) {
                if (checkedmovie.equals(movie)) {

                    UserCheckedMovies.get(user).remove(movie);
                    if (UserCheckedMovies.get(user).size() == 0) {
                        UserCheckedMovies.remove(user);
                    }
                    return true;

                }
            }
        }
       return false;
    }

    public Movie getMovieFromList(String name) {
        int pointer = 0;
        while (pointer < movies.size()) {
            if (name.equalsIgnoreCase(movies.get(pointer).getMovieName())) {
                return movies.get(pointer);
            }


            pointer++;
        }
        return null;
    }


    public List<Movie> getAvailableMovies()
    {
        ArrayList<Movie> availableMovies=new ArrayList<Movie>();
        for (Movie movie :movies) {
            if(checked_out_movies.contains(movie))
            {
                continue;
            }
            availableMovies.add(movie);

        }

        return availableMovies;
    }

    //user
    public Map<String,List<Book>> getCheckedOutBooks()
    {

        return UserCheckedBooks;
    }
    public Map<String,List<Movie>> getCheckedOutMovies()
    {

        return UserCheckedMovies;
    }

    public Map<String, User> getRegisteredMembers() {
        return registeredMembers;
    }


    public String getUserProfile(String user)
    {
        //return "library number: "+user+" name: "+registeredMembers.get(user).getName()+" email: "+registeredMembers.get(user).getEmail()+" phone: "+registeredMembers.get(user).getPhone();
        return "LIBRARY NUMBER :  "+user+" "+registeredMembers.get(user).toString();
    }

}

